home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_2 / newton.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  903 b   |  33 lines  |  [MATF/MATL]

  1. function [p0,y0,err,P] = newton(f,df,p0,delta,epsilon,max1) 
  2. % [p0,y0,err] = newton(f,df,p0,delta,epsilon,max1)
  3. % [p0,y0,err,P] = newton(f,df,p0,delta,epsilon,max1)
  4. % Newton's method is used to locate a root.
  5. % f  is the function, input.
  6. % df is the derivative of f, input.
  7. % p0 is the starting point, input.
  8. % delta is the tolerance for p0, input.
  9. % epsilon is the tolerance for y0, input.
  10. % max1 is the maximum number of iterations, input.
  11. % p0 is the root, output.
  12. % y0 is the function value, output.
  13. % err is the error estimate for p0, output.
  14. % P is the is the vector of iterations, output.
  15. P(1) = p0;
  16. y0 = feval(f,p0);
  17. for k=1:max1,
  18.   df0 = feval(df,p0);
  19.   if df0 == 0,
  20.     dp = 0;
  21.   else
  22.     dp = y0/df0;
  23.   end
  24.   p1 = p0 - dp;
  25.   y1 = feval(f,p1);
  26.   err = abs(dp);
  27.   relerr = err/(abs(p1)+eps);
  28.   p0 = p1;
  29.   y0 = y1;
  30.   P = [P;p1];
  31.   if (err<delta)|(relerr<delta)|(abs(y1)<epsilon), break, end
  32. end
  33.